import { AfterViewInit, Component, Injector, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { DictTypeService } from '../service/dict_type.service';
import { BaseUI } from '../../../../common/base';
import { listAnimation } from '../../../../animations/list.animation';
import { DictTypeOptionComponent } from '../component/dict-type-option/dict-type-option.component';
import { MainTableComponent } from '../../../../components/nested-table/main-table/main-table.component';
import { DictDataService } from '../service/dict-data.service';
import { ColumnType } from '../../../../components/table/enums/ColumnType';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../../../components/table/table-option/table-option.model';
import { DictDataOptionComponent } from '../component/dict-data-option/dict-data-option.component';
import { DateUtil } from '../../../../utils/DateUtil';

@Component({
    selector: 'app-dict',
    templateUrl: './dict.component.html',
    styleUrls: ['./dict.component.scss'],
    animations: [listAnimation]
})
export class DictComponent extends BaseUI implements OnInit, AfterViewInit, OnDestroy {

    searchObj:any = {
        pageNum: 1,
        pageSize: this._config.pageSize,
    };

    constructor(
        public _config: ConfigModule,
        private _injector: Injector,
        public dictTypeService: DictTypeService,
        public dictDataService: DictDataService,
    ) {
        super(_injector);
    }

    ngOnInit() {
    }

    ngAfterViewInit() {
    }

    ngOnDestroy() {
    }



    @ViewChild('tableComponent') tableComponent: MainTableComponent;
    @ViewChild('dictTypeOptionComponent') dictTypeOptionComponent: DictTypeOptionComponent;
    @ViewChild('dictDataOptionComponent') dictDataOptionComponent: DictDataOptionComponent;
    dictTypeColumnArray = [
        { type: ColumnType.TEXT, name: '字典名称', key: 'dictName' },
        { type: ColumnType.TEXT, name: '字典标志', key: 'dictType' },
        { type: ColumnType.LABEL, name: '字典状态', key: 'status' },
        { type: ColumnType.TEXT, name: '备注', key: 'remark' },
        { type: ColumnType.TEXT, name: '创建时间', key: 'createTime' },
    ];
    dictDataColumnArray = [
        { type: ColumnType.TEXT, name: '字典标签', key: 'dictLabel' },
        { type: ColumnType.TEXT, name: '字典键值', key: 'dictValue' },
        { type: ColumnType.TEXT, name: '字典排序', key: 'dictSort' },
        { type: ColumnType.LABEL, name: '字典状态', key: 'status' },
        { type: ColumnType.TEXT, name: '备注', key: 'remark' },
        { type: ColumnType.TEXT, name: '创建时间', key: 'createTime' },

    ];
    optionModelList: TableOptionModel[] = [
        {
            optionLabel:"修改",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.EDIT
        },
        {
            optionLabel:"删除",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.DELETE
        }
    ]
    subOptionModelList: TableOptionModel[] = [
        {
            optionLabel:"修改",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.EDIT
        },
        {
            optionLabel:"删除",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.DELETE
        }
    ]

    subOtherOptionModelList: TableOptionModel[] = [
        {
            optionLabel:"新增",
            optionLabelType: OptionTypeEnum.BUTTON,
            eventType:"",
            eventKey:EventKeyEnum.ADD
        },
    ]

    optionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.EDIT:
                this.dictTypeOptionComponent.onModalOpen(data.dictId);
                break;
            case EventKeyEnum.ADD:
                this.dictTypeOptionComponent.onModalOpen("");
                break;
        }
    }

    subOptionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.EDIT:
                this.dictDataOptionComponent.onModalOpen(data.dictCode,data.dictType);
                break;
            case EventKeyEnum.ADD:
                this.dictDataOptionComponent.onModalOpen("",data.dictType);
                break;
        }
    }

    /**
     * 日期选择
     */

    datePick: string[]=[];
    onDatePick() {
        this.searchObj.params = {};
        this.searchObj.params['beginTime'] = DateUtil.formatDate(this.datePick[0],"yyyy-MM-dd");
        this.searchObj.params['endTime'] = DateUtil.formatDate(this.datePick[1],"yyyy-MM-dd");
    }

    resetSearchObject(){
        this.searchObj = {
            pageNum: 1,
            pageSize: this._config.pageSize,
        };
        this.datePick=[];
    }

    keyEnter($event: KeyboardEvent) {
        if($event.key === 'Enter'){
            this.tableComponent.list();
        }
    }
}
